package com.approval.approvalapp.index.dbsx.wzsp.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approval.WzspLcAdapter;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.WzspXqBean;
import com.approval.approvalapp.index.dbsx.tshf.BaseActivity2;
import com.approval.approvalapp.index.dbsx.wzsp.adapter.WzxqRvAdapter;
import com.approval.approvalapp.index.dbsx.wzsp.fragment.WzSpDbFragment;
import com.approval.approvalapp.index.dbsx.wzsp.fragment.WzSpYbFragment;
import com.approval.approvalapp.myself.activity.WdHysSqActivity;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WzspxqDbActivity extends BaseActivity2 {
    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.name_tv)
    TextView nameTv;
    @Bind(R.id.rv)
    RecyclerView rv;
//    @Bind(R.id.wzfl_tv)
//    TextView wzflTv;
//    @Bind(R.id.slhj_tv)
//    TextView slhjTv;
//    @Bind(R.id.slwz_tv)
//    TextView slwzTv;
    @Bind(R.id.ytsm_tv)
    TextView ytsmTv;
    @Bind(R.id.refuse_tv)
    LinearLayout refuseTv;
    @Bind(R.id.agree_tv)
    LinearLayout agreeTv;
    private String id;
    @Bind(R.id.rv2)
    RecyclerView rv2;


    @Override
    public int getLayoutId() {
        return R.layout.activity_wzspxq_db;
    }

    @Override
    public void initView() {
        String name = getIntent().getStringExtra("name");
        titleTv1.setText(name + "的物资领用");
        nameTv.setText(name);
        id = getIntent().getStringExtra("id");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", id);
        Log.d("WzspxqDbActivitysss", id);
        NetRequsetUtil.getInstance().netRequestPostJson("v1/center/material_already_apply",
                jsonObject.toString(),
                new NetDownResponse() {
                    @Override
                    public void success(String str) {
                        WzspXqBean wzspXqBean = JSON.parseObject(str, WzspXqBean.class);
//                        wzflTv.setText(wzspXqBean.getShow().getType_text());
//                        slhjTv.setText(wzspXqBean.getShow().getContent() + "件");
//                        slwzTv.setText(wzspXqBean.getShow().getMaterial());
//                        ytsmTv.setText(wzspXqBean.getShow().getContent());


                        if (rv == null){
                            return;
                        }

                        rv.setLayoutManager(new LinearLayoutManager(WzspxqDbActivity.this));
                        WzxqRvAdapter wzxqRvAdapter = new WzxqRvAdapter();
                        wzxqRvAdapter.setListBeanXES(wzspXqBean.getList());
                        rv.setAdapter(wzxqRvAdapter);

                        rv2.setLayoutManager(new LinearLayoutManager(WzspxqDbActivity.this));
                        WzspLcAdapter wzspLcAdapter = new WzspLcAdapter();
                        wzspLcAdapter.setList(wzspXqBean.getProcess());
                        rv2.setAdapter(wzspLcAdapter);
                    }

                    @Override
                    public void errowithresponse(String str) {

                    }

                    @Override
                    public void erro() {

                    }

                    @Override
                    public void finish() {

                    }
                });
    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.refuse_tv, R.id.agree_tv, R.id.back_iv})
    public void onViewClicked(View view) {
        JSONObject jsonObject = new JSONObject();
        switch (view.getId()) {
            case R.id.refuse_tv:
                if (SpUtil.getString(this,
                        SpUtil.Usertype, "").equals("1")) {
                    //zx
                    if (WzSpActivity.instance.uriid.equals("3")) {
                        jsonObject.put("opt", "0");
                        jsonObject.put("type", "2");
                    } else {
                        jsonObject.put("opt", "1");
                        jsonObject.put("type", "4");
                    }
                } else {
                    jsonObject.put("opt", "0");
                    jsonObject.put("type", "2");
                }
                jsonObject.put("id", id);
                jsonObject.put("wd_code", getIntent().getStringExtra("wdcode"));
                jsonObject.put("name", SpUtil.getString(this,
                        SpUtil.USER_RUN_NICKNAME, ""));
                Log.d("WzspxqDbActivssity", "refuse:" + jsonObject);
                NetRequsetUtil.getInstance()
                        .netRequestPostJson("v1/center/yes_no_material",
                                jsonObject.toString(),
                                new NetDownResponse() {
                                    @Override
                                    public void success(String str) {
                                        Toast.makeText(WzspxqDbActivity.this, "拒绝成功", Toast.LENGTH_SHORT).show();
                                        WzspxqDbActivity.this.finish();
                                        if (WzSpDbFragment.instance != null){
                                            WzSpDbFragment.instance.refreshData();
                                        }
                                        if (WzSpYbFragment.instance != null){
                                            WzSpYbFragment.instance.refreshData();
                                        }
                                    }

                                    @Override
                                    public void errowithresponse(String str) {
                                        ErrorBeanUtil.getErrorMessage(str);

                                    }

                                    @Override
                                    public void erro() {

                                    }

                                    @Override
                                    public void finish() {

                                    }
                                });
                break;
            case R.id.agree_tv:
                if (SpUtil.getString(this,
                        SpUtil.Usertype, "").equals("1")) {
                    //zx
                    if (WzSpActivity.instance.uriid.equals("3")) {
                        jsonObject.put("opt", "0");
                        jsonObject.put("type", "1");
                    } else {
                        jsonObject.put("opt", "1");
                        jsonObject.put("type", "3");
                    }
                } else {
                    jsonObject.put("opt", "0");
                    jsonObject.put("type", "1");
                }
                jsonObject.put("id", id);
                jsonObject.put("wd_code", getIntent().getStringExtra("wdcode"));
                jsonObject.put("name", SpUtil.getString(this,
                        SpUtil.USER_RUN_NICKNAME, ""));
                Log.d("WzspxqDbActivssity", "agree:" + jsonObject);
                NetRequsetUtil.getInstance()
                        .netRequestPostJson("v1/center/yes_no_material",
                                jsonObject.toString(),
                                new NetDownResponse() {
                                    @Override
                                    public void success(String str) {
                                        Toast.makeText(WzspxqDbActivity.this, "同意成功", Toast.LENGTH_SHORT).show();
                                        WzspxqDbActivity.this.finish();
                                        if (WzSpDbFragment.instance != null){
                                            WzSpDbFragment.instance.refreshData();
                                        }
                                        if (WzSpYbFragment.instance != null){
                                            WzSpYbFragment.instance.refreshData();
                                        }
                                    }

                                    @Override
                                    public void errowithresponse(String str) {
                                        ErrorBeanUtil.getErrorMessage(str);

                                    }

                                    @Override
                                    public void erro() {

                                    }

                                    @Override
                                    public void finish() {

                                    }
                                });
                break;
            case R.id.back_iv:
                finish();
                break;
        }
    }
}
